<template>
  <footer>
    <div v-if="btnFlag" class="cd-top" @click="backTop">
      <span class="iconfont">&#xe64b;</span>
    </div>
    <p>
      Design by
      <a href="/">XXX</a>
      <a href="/">蜀ICP备11002373号-1</a>
    </p>
  </footer>
</template>
<script>
export default {
  data() {
    return {
      btnFlag: false
    };
  },
  mounted() {
    window.addEventListener("scroll", this.scrollToTop);
  },
  methods: {
    backTop() {
      const that = this;
      let timer = setInterval(() => {
        let ispeed = Math.floor(-that.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          that.scrollTop + ispeed;
        if (that.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 16);
    },
    // 为了计算距离顶部的高度，当高度大于60显示回顶部图标，小于60则隐藏
    scrollToTop() {
      const that = this;
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      that.scrollTop = scrollTop;
      if (that.scrollTop > 60) {
        that.btnFlag = true;
      } else {
        that.btnFlag = false;
      }
    }
  }
};
</script>
<style lang="scss">
@import "@/assets/scss/base.scss";
@import "@/assets/scss/index.scss";
@import "@/assets/scss/m.scss";

.iconfont {
  font-size: 24px;
}

.cd-top {
  background-color: #444;
  line-height: 40px;
  height: 40px;
  width: 40px;
  position: fixed;
  bottom: 68px;
  right: 12px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
  white-space: nowrap;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
  z-index: 9999999;
  cursor: pointer;
}
</style>
